package cn.tedu.suppermemory.controller;

import cn.tedu.suppermemory.pojo.dto.ScoreAddNewDTO;
import cn.tedu.suppermemory.pojo.dto.ScoreUpdateDTO;
import cn.tedu.suppermemory.pojo.vo.ScoreListItemVO;
import cn.tedu.suppermemory.pojo.vo.ScoreStandardVO;
import cn.tedu.suppermemory.service.IScoreService;
import cn.tedu.suppermemory.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "2.学生成绩")
@Slf4j
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private IScoreService scoreService;

    public ScoreController() {
        log.debug("创建控制器类对象：ScoreController");
    }

    // http://localhost:9080/albums/add-new?name=TestAlbum001&description=TestDescription001&sort=88
    @PostMapping("/add-new")
    @ApiOperation("添加学生成绩")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid ScoreAddNewDTO scoreAddNewDTO) {
        log.debug("开始处理【添加学生成绩】的请求，参数：{}", scoreAddNewDTO);
        scoreService.addNew(scoreAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除学生成绩")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "学生成绩ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000000, message = "删除学生成绩失败，学生成绩ID非法！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除课程】的请求，参数：{}", id);
        scoreService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改学生成绩详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, ScoreUpdateDTO scoreUpdateDTO) {
        log.debug("开始处理【修改学生成绩详情】的请求，参数：{}", scoreUpdateDTO);
        scoreService.updateInfoById(id, scoreUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询学生成绩详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "学生成绩ID", required = true, example = "9527", dataType = "long")
    public JsonResult<ScoreStandardVO> getStandardById(@Range(min = 1, max = 1000000, message = "查询课程详情失败，相册ID非法！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询学生成绩详情】的请求，参数：{}", id);
        ScoreStandardVO scoreStandardVO = scoreService.getStandardById(id);
        return JsonResult.ok(scoreStandardVO);
    }

    // http://localhost:9080/albums
    @GetMapping("")
    @ApiOperation("查询学生成绩列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<ScoreListItemVO>> list() {
        log.debug("开始处理【查询学生成绩列表】的业务，无参数");
        List<ScoreListItemVO> list = scoreService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/albums/aaa
    @Deprecated
    @GetMapping("/{classId:[a-z]+}")
    public String getStandardByName(@PathVariable Long classId) {
        return "根据ClassId【" + classId + "】查询课程详情";
    }

    // http://localhost:9080/albums/test
    @Deprecated
    @GetMapping("/test")
    public String testStandardByName(Long classId) {
        return "【测试】根据ClassId【" + classId + "】查询课程详情";
    }


}
